package com.qxzn.app.page;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qxzn.app.api.util.EventBusManager;
import com.qxzn.app.bean.EventBusBean;
import com.qxzn.app.page.view.ProgressDialog;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment extends Fragment {

    private Unbinder unbinder;
    private ProgressDialog progressDialog;

    protected ProgressDialog showProgress() {
        return showProgress(true);
    }

    protected ProgressDialog showProgress(boolean cancelable) {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(getContext(), cancelable, false);
        }
        if (!progressDialog.isShowing()) {
            if (!this.isRemoving()) {//如果页面没有被关闭则往下执行
                progressDialog.show();
            }
        }
        return progressDialog;
    }

    protected void hideProgress() {
        if (progressDialog != null && progressDialog.isShowing()) {
            if (!this.isRemoving()) {
                progressDialog.dismiss();
            }
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(initLayout(), container, false);
        unbinder = ButterKnife.bind(this, view);
        EventBusManager.register(this);
        initView();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData(savedInstanceState);
    }

    //设置加载的布局
    protected abstract int initLayout();

    //初始化控件
    protected abstract void initView();

    //初始化数据
    protected abstract void initData(Bundle savedInstanceState);

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBus(EventBusBean busBean) {
        if (busBean.getId() == 1) {
        }
    }

    protected void startActivity(Class<?> cls) {
        Intent intent = new Intent(getActivity(), cls);
        startActivity(intent);
    }

    @Override
    public void onResume() {
        super.onResume();
        onFragmentResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        onFragmentPause();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            onFragmentResume();
        } else {
            onFragmentPause();
        }
    }

    protected void onFragmentResume() {

    }

    protected void onFragmentPause() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBusManager.unregister(this);
    }
}
